<!DOCTYPE html><html class="theme-next gemini use-motion" lang="zh-Hans"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1"><meta name="theme-color" content="#222"><script src="//cdn.bootcss.com/pace/1.0.2/pace.min.js"></script><link href="//cdn.bootcss.com/pace/1.0.2/themes/pink/pace-theme-flash.css" rel="stylesheet"><style>.pace .pace-progress{background:#1e92fb;height:3px}.pace .pace-progress-inner{box-shadow:0 0 10px #1e92fb,0 0 5px #1e92fb}.pace .pace-activity{border-top-color:#1e92fb;border-left-color:#1e92fb}</style><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css"><link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css"><link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css"><link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4"><link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.ico?v=5.1.4"><link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4"><link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222"><meta name="keywords" content="ReactNative,"><link rel="alternate" href="/atom.xml" title="It is written" type="application/atom+xml"><meta name="description" content="介绍React Navigation，路由导航器，用来实现多个页面之间的跳转。 本次讲解最新版本 V5。 官方文档：https:&#x2F;&#x2F;reactnavigation.org&#x2F;"><meta property="og:type" content="article"><meta property="og:title" content="ReactNative -- React Navigation 路由导航"><meta property="og:url" content="http://king-xiyu.gitee.io/article/92cd96a1.html"><meta property="og:site_name" content="It is written"><meta property="og:description" content="介绍React Navigation，路由导航器，用来实现多个页面之间的跳转。 本次讲解最新版本 V5。 官方文档：https:&#x2F;&#x2F;reactnavigation.org&#x2F;"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1gegg4q8l74j31a70ozdiy.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1gegg5g4q2lj31g80pqkef.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1gegg5v813oj31220b0dqi.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1gegg6azzm9j31900pkwkb.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1gegg6sh6l4j306i048t9d.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1gegg74axe6j30wc0ls135.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1gegg7n5ljlj30ny03gmyp.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1gegg85m8bvj30ok05y0vh.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1gegg8hvayfj30me0qu139.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1gegg8vzjmoj30i40vm0tu.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1gegg97q6brj31zg17gkjl.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1gegg9l9hm9j308y03wdgn.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1gegg9zqp3mj31l210a4qp.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggabijblj31040w21j1.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggamy23hj31qc0m4aow.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggavzajij30hk06ywf1.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggb3s0ssj30hs04sgls.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggbdobymj313e0c8afl.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggbl9lr4j30ws0c8jw4.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggbrso43j31sy0uo4lf.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggbxwwsuj31rk0p4aoz.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggc5k306j30ji0f440q.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggchswmrj31780hg180.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggcpnmxdj31c60zckaq.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggcy3heoj31f00vwnhr.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggd7wr5lj31f212kx23.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggdbnjetj30jo05i0u0.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggdkr5rpj31mo0zekik.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggdxrl71j31j01bwe81.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1gegge2c211j30wo0fowkj.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggei953gj31ns11c1kx.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggetpz8ij307u0cgtax.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggez4ywhj30rs1527qq.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggf5tfv0j314m1347qg.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggfd5bpdj30i20zg0wl.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggfhugzjj30ho0voq4d.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggfrc2m9j30di08mjt9.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggfzm1w3j312k0g4tju.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggg6jrx4j316i0piapv.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggger2nsj30ii0t4405.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggijx9fgj30ae07agn2.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggipjrxkj30ts0uo1ap.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggitozccj314u1601ij.jpg"><meta property="og:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1geggiygbnhj30i20rymyl.jpg"><meta property="article:published_time" content="2020-04-27T07:07:24.000Z"><meta property="article:modified_time" content="2020-09-01T00:45:13.776Z"><meta property="article:author" content="King-XiYu"><meta property="article:tag" content="ReactNative"><meta name="twitter:card" content="summary"><meta name="twitter:image" content="http://ww1.sinaimg.cn/large/007WurYGgy1gegg4q8l74j31a70ozdiy.jpg"><script type="text/javascript" id="hexo.configurations">var NexT=window.NexT||{},CONFIG={root:"/",scheme:"Gemini",version:"5.1.4",sidebar:{position:"left",display:"post",offset:12,b2t:!0,scrollpercent:!0,onmobile:!1},fancybox:!0,tabs:!0,motion:{enable:!0,async:!1,transition:{post_block:"fadeIn",post_header:"slideDownIn",post_body:"slideDownIn",coll_header:"slideLeftIn",sidebar:"slideUpIn"}},duoshuo:{userId:"0",author:"博主"},algolia:{applicationID:"",apiKey:"",indexName:"",hits:{per_page:10},labels:{input_placeholder:"Search for Posts",hits_empty:"We didn't find any results for the search: ${query}",hits_stats:"${hits} results found in ${time} ms"}}}</script><link rel="canonical" href="http://king-xiyu.gitee.io/article/92cd96a1.html"><title>ReactNative -- React Navigation 路由导航 | It is written</title><meta name="generator" content="Hexo 4.2.0"></head><body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans"><div class="container sidebar-position-left page-post-detail"><div class="headband"></div><header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="header-inner"><div class="site-brand-wrapper"><div class="site-meta"><div class="custom-logo-site-title"><a href="/" class="brand" rel="start"><span class="logo-line-before"><i></i></span> <span class="site-title">It is written</span><span class="logo-line-after"><i></i></span></a></div><p class="site-subtitle"></p></div><div class="site-nav-toggle"> <button><span class="btn-bar"></span><span class="btn-bar"></span><span class="btn-bar"></span></button></div></div><nav class="site-nav"><ul id="menu" class="menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i><br> 首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i><br> 标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i><br> 分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i><br> 归档</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="menu-item-icon fa fa-fw fa-user"></i><br> 关于</a></li><li class="menu-item menu-item-search"><a href="javascript:;" class="popup-trigger"><i class="menu-item-icon fa fa-search fa-fw"></i><br> 搜索</a></li></ul><div class="site-search"><div class="popup search-popup local-search-popup"><div class="local-search-header clearfix"><span class="search-icon"><i class="fa fa-search"></i></span><span class="popup-btn-close"><i class="fa fa-times-circle"></i></span><div class="local-search-input-wrapper"> <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input"></div></div><div id="local-search-result"></div></div></div></nav></div></header><main id="main" class="main"><div class="main-inner"><div class="content-wrap"><div id="content" class="content"><div id="posts" class="posts-expand"><article class="post post-type-normal" itemscope itemtype="http://schema.org/Article"><div class="post-block"><link itemprop="mainEntityOfPage" href="http://king-xiyu.gitee.io/article/92cd96a1.html"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="name" content="King-XiYu"><meta itemprop="description" content=""><meta itemprop="image" content="http://ww1.sinaimg.cn/large/007WurYGgy1ge8nxttd9nj302s02s0si.jpg"></span><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="It is written"></span><header class="post-header"><h1 class="post-title" itemprop="name headline">ReactNative -- React Navigation 路由导航</h1><div class="post-meta"><span class="post-time"><span class="post-meta-item-icon"><i class="fa fa-calendar-o"></i></span> <span class="post-meta-item-text">发表于</span> <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-04-27T15:07:24+08:00">2020-04-27</time> <span class="post-meta-divider">|</span><span class="post-meta-item-icon"><i class="fa fa-calendar-check-o"></i></span> <span class="post-meta-item-text">更新于&#58;</span> <time title="更新于" itemprop="dateModified" datetime="2020-09-01T08:45:13+08:00">2020-09-01</time></span> <span class="post-category"><span class="post-meta-divider">|</span><span class="post-meta-item-icon"><i class="fa fa-folder-o"></i></span> <span class="post-meta-item-text">分类于</span> <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/%E6%8A%80%E6%9C%AF%E6%A0%88/" itemprop="url" rel="index"><span itemprop="name">技术栈</span></a></span> ， <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/%E6%8A%80%E6%9C%AF%E6%A0%88/ReactNative/" itemprop="url" rel="index"><span itemprop="name">ReactNative</span></a></span></span> <span class="post-comments-count"><span class="post-meta-divider">|</span><span class="post-meta-item-icon"><i class="fa fa-comment-o"></i></span><a href="/article/92cd96a1.html#comments" itemprop="discussionUrl"><span class="post-comments-count valine-comment-count" data-xid="/article/92cd96a1.html" itemprop="commentCount"></span></a></span> <span id="/article/92cd96a1.html" class="leancloud_visitors" data-flag-title="ReactNative -- React Navigation 路由导航"><span class="post-meta-divider">|</span><span class="post-meta-item-icon"><i class="fa fa-eye"></i></span> <span class="post-meta-item-text">阅读次数&#58;</span><span class="leancloud-visitors-count"></span></span><div class="post-wordcount"><span class="post-meta-item-icon"><i class="fa fa-file-word-o"></i></span> <span class="post-meta-item-text">字数统计&#58;</span> <span title="字数统计">2.6k 字</span> <span class="post-meta-divider">|</span><span class="post-meta-item-icon"><i class="fa fa-clock-o"></i></span> <span class="post-meta-item-text">阅读时长 &asymp;</span> <span title="阅读时长">10 分钟</span></div></div></header><div class="post-body" itemprop="articleBody"><h1 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h1><p>React Navigation，路由导航器，用来实现多个页面之间的跳转。</p><p>本次讲解最新版本 V5。</p><p>官方文档：<a href="https://reactnavigation.org/" target="_blank" rel="noopener">https://reactnavigation.org/</a></p><a id="more"></a><h1 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h1><ol><li>安装基础包</li></ol><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn add @react-navigation&#x2F;native</span><br></pre></td></tr></table></figure><ol start="2"><li>安装依赖包</li></ol><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn add react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community&#x2F;masked-view</span><br></pre></td></tr></table></figure><ol start="3"><li>在入口文件的 第 1 行（前面不能再有代码）引入</li></ol><p>index.js</p><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="string">'react-native-gesture-handler'</span>;</span><br><span class="line"></span><br><span class="line">...</span><br></pre></td></tr></table></figure><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1gegg4q8l74j31a70ozdiy.jpg" alt="Snipaste_2020-04-22_16-59-03.png"></p><ol start="4"><li>整个 APP 最外层要使用 <code>NavigationContainer</code> 组件套起来</li></ol><p>App.js</p><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1gegg5g4q2lj31g80pqkef.jpg" alt="image-20200421144352037.png"></p><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">'react'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; StyleSheet, View, Text &#125; <span class="keyword">from</span> <span class="string">'react-native'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; NavigationContainer &#125; <span class="keyword">from</span> <span class="string">'@react-navigation/native'</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">App</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;NavigationContainer&gt;</span><br><span class="line">      &lt;Text&gt;</span><br><span class="line">        Hello World~</span><br><span class="line">      &lt;<span class="regexp">/Text&gt;</span></span><br><span class="line"><span class="regexp">    &lt;/</span>NavigationContainer&gt;</span><br><span class="line">  )</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> styles = StyleSheet.create(&#123;&#125;);</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> App;</span><br></pre></td></tr></table></figure><h1 id="五种导航器"><a href="#五种导航器" class="headerlink" title="五种导航器"></a>五种导航器</h1><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1gegg5v813oj31220b0dqi.jpg" alt="image-20200422101224846.png"></p><p>效果可以到官方查看 gif 动图：</p><p><a href="https://reactnavigation.org/docs/material-top-tab-navigator#" target="_blank" rel="noopener">https://reactnavigation.org/docs/material-top-tab-navigator#</a>!</p><p>我们在管理多个页面时就可以选择其中一种来管理页面。</p><p>设计器设计的很优秀，五种导航器在使用时代码结构完全一样。</p><h2 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h2><p>这五种导航器里的底部类导航器会带有图标，如果是新创建的RN项目是不带有icon图标配置的，</p><p>还需要我们手动配置一下，</p><ol><li>安装</li></ol><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install --save react-native-vector-icons</span><br></pre></td></tr></table></figure><ol start="2"><li>配置</li></ol><p>在 Android 系统中要使用图标需要配置。</p><p>修改 <code>android/app/build.gradle</code> 文件，添加一行到最后：</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">apply from: &quot;..&#x2F;..&#x2F;node_modules&#x2F;react-native-vector-icons&#x2F;fonts.gradle&quot;</span><br></pre></td></tr></table></figure><ol start="3"><li>选择要使用的图片类</li></ol><p>图标查看地址：<a href="https://oblador.github.io/react-native-vector-icons/" target="_blank" rel="noopener">https://oblador.github.io/react-native-vector-icons/</a></p><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1gegg6azzm9j31900pkwkb.jpg" alt="image-20200420170307269.png"></p><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Icon <span class="keyword">from</span> <span class="string">'react-native-vector-icons/Feather'</span>;</span><br><span class="line"></span><br><span class="line">&lt;Icon name=<span class="string">"aperture"</span> size=&#123;<span class="number">30</span>&#125; color=<span class="string">"#900"</span> /&gt;;</span><br></pre></td></tr></table></figure><ol start="4"><li>重新打包项目</li></ol><h2 id="示例"><a href="#示例" class="headerlink" title="示例"></a>示例</h2><p>分别使用五种导航器来管理三个页面</p><h3 id="1-创建三个页面"><a href="#1-创建三个页面" class="headerlink" title="1. 创建三个页面"></a>1. 创建三个页面</h3><p>创建页时快捷键(rnfs)</p><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1gegg6sh6l4j306i048t9d.jpg" alt="image-20200422101705066.png"></p><h3 id="2-安装五种导航器"><a href="#2-安装五种导航器" class="headerlink" title="2. 安装五种导航器"></a>2. 安装五种导航器</h3><p>每个导航器在使用之前需要先安装一下，需要哪个安装哪个？</p><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 栈导航器</span></span><br><span class="line">yarn add @react-navigation/stack</span><br><span class="line"><span class="comment">// 抽屉导航器</span></span><br><span class="line">yarn add @react-navigation/drawer</span><br><span class="line"><span class="comment">// 底部导航器</span></span><br><span class="line">yarn add @react-navigation/bottom-tabs</span><br><span class="line"><span class="comment">// 更丰富的底部导航器</span></span><br><span class="line">yarn add @react-navigation/material-bottom-tabs react-native-paper</span><br><span class="line"><span class="comment">// 顶部导航器</span></span><br><span class="line">yarn add @react-navigation/material-top-tabs react-native-tab-view</span><br></pre></td></tr></table></figure><h3 id="3-引入导航器容器，套在最外层"><a href="#3-引入导航器容器，套在最外层" class="headerlink" title="3. 引入导航器容器，套在最外层"></a>3. 引入导航器容器，套在最外层</h3><p>App.js</p><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1gegg74axe6j30wc0ls135.jpg" alt="image-20200422102136827.png"></p><h3 id="4-引入五个导航器（用哪个引哪个）"><a href="#4-引入五个导航器（用哪个引哪个）" class="headerlink" title="4. 引入五个导航器（用哪个引哪个）"></a>4. 引入五个导航器（用哪个引哪个）</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 引入五个导航器</span><br><span class="line">import &#123; createStackNavigator &#125; from &#39;@react-navigation&#x2F;stack&#39;;</span><br><span class="line">import &#123; createDrawerNavigator &#125; from &#39;@react-navigation&#x2F;drawer&#39;;</span><br><span class="line">import &#123; createBottomTabNavigator &#125; from &#39;@react-navigation&#x2F;bottom-tabs&#39;;</span><br><span class="line">import &#123; createMaterialBottomTabNavigator &#125; from &#39;@react-navigation&#x2F;material-bottom-tabs&#39;;</span><br><span class="line">import &#123; createMaterialTopTabNavigator &#125; from &#39;@react-navigation&#x2F;material-top-tabs&#39;;</span><br></pre></td></tr></table></figure><h3 id="5-创建导航器组件（用哪个创建哪个，这里我们先试一下-Drawer）"><a href="#5-创建导航器组件（用哪个创建哪个，这里我们先试一下-Drawer）" class="headerlink" title="5. 创建导航器组件（用哪个创建哪个，这里我们先试一下 Drawer）"></a>5. 创建导航器组件（用哪个创建哪个，这里我们先试一下 Drawer）</h3><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1gegg7n5ljlj30ny03gmyp.jpg" alt="image-20200422102451026.png"></p><h3 id="6-引入所有的页面"><a href="#6-引入所有的页面" class="headerlink" title="6. 引入所有的页面"></a>6. 引入所有的页面</h3><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1gegg85m8bvj30ok05y0vh.jpg" alt="image-20200422102347479.png"></p><h3 id="7-使用这个组件来管理页面"><a href="#7-使用这个组件来管理页面" class="headerlink" title="7. 使用这个组件来管理页面"></a>7. 使用这个组件来管理页面</h3><p>在导航器组件中还包含两个组件：<code>Navigator</code> 和 <code>Screen</code>。</p><p>Navigator：父组件，要把所有页面套起来。</p><p>Sceen：在 Navigator 中定义每个页面</p><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1gegg8hvayfj30me0qu139.jpg" alt="image-20200422102806164.png"></p><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">'react'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; StyleSheet, View, Text &#125; <span class="keyword">from</span> <span class="string">'react-native'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 引入导航容器</span></span><br><span class="line"><span class="keyword">import</span> &#123; NavigationContainer &#125; <span class="keyword">from</span> <span class="string">'@react-navigation/native'</span></span><br><span class="line"><span class="comment">// 引入五个导航器</span></span><br><span class="line"><span class="keyword">import</span> &#123; createStackNavigator &#125; <span class="keyword">from</span> <span class="string">'@react-navigation/stack'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; createDrawerNavigator &#125; <span class="keyword">from</span> <span class="string">'@react-navigation/drawer'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; createBottomTabNavigator &#125; <span class="keyword">from</span> <span class="string">'@react-navigation/bottom-tabs'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; createMaterialBottomTabNavigator &#125; <span class="keyword">from</span> <span class="string">'@react-navigation/material-bottom-tabs'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; createMaterialTopTabNavigator &#125; <span class="keyword">from</span> <span class="string">'@react-navigation/material-top-tabs'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 创建导航器组件</span></span><br><span class="line"><span class="keyword">const</span> DrawerNavigator = createDrawerNavigator()</span><br><span class="line"></span><br><span class="line"><span class="comment">// 引入页面</span></span><br><span class="line"><span class="keyword">import</span> Index <span class="keyword">from</span> <span class="string">'./pages/Index'</span></span><br><span class="line"><span class="keyword">import</span> Me <span class="keyword">from</span> <span class="string">'./pages/Me'</span></span><br><span class="line"><span class="keyword">import</span> Detail <span class="keyword">from</span> <span class="string">'./pages/Detail'</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> App = <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;NavigationContainer&gt;</span><br><span class="line">      &lt;DrawerNavigator.Navigator&gt;</span><br><span class="line">        &lt;DrawerNavigator.Screen</span><br><span class="line">          name=<span class="string">"Index"</span></span><br><span class="line">          component=&#123;Index&#125;</span><br><span class="line">          options=&#123;&#123; <span class="attr">title</span>: <span class="string">'首页'</span> &#125;&#125;</span><br><span class="line">        /&gt;</span><br><span class="line">        &lt;DrawerNavigator.Screen</span><br><span class="line">          name=<span class="string">"Detail"</span></span><br><span class="line">          component=&#123;Detail&#125;</span><br><span class="line">          options=&#123;&#123; <span class="attr">title</span>: <span class="string">'详情'</span> &#125;&#125;</span><br><span class="line">        /&gt;</span><br><span class="line">        &lt;DrawerNavigator.Screen</span><br><span class="line">          name=<span class="string">"Me"</span></span><br><span class="line">          component=&#123;Me&#125;</span><br><span class="line">          options=&#123;&#123; <span class="attr">title</span>: <span class="string">'我的'</span> &#125;&#125;</span><br><span class="line">        /&gt;</span><br><span class="line">      &lt;<span class="regexp">/DrawerNavigator.Navigator&gt;</span></span><br><span class="line"><span class="regexp">    &lt;/</span>NavigationContainer&gt;</span><br><span class="line">  )</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> styles = StyleSheet.create(&#123;&#125;);</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> App;</span><br></pre></td></tr></table></figure><h3 id="效果："><a href="#效果：" class="headerlink" title="效果："></a>效果：</h3><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1gegg8vzjmoj30i40vm0tu.jpg" alt="image-20200422102909477.png"></p><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1gegg97q6brj31zg17gkjl.jpg" alt="image-20200422103037038.png"></p><h1 id="栈导航器"><a href="#栈导航器" class="headerlink" title="栈导航器"></a>栈导航器</h1><h2 id="注册页面"><a href="#注册页面" class="headerlink" title="注册页面"></a>注册页面</h2><p>首先我们需要先创建几个页面，然后把这个页面注册到导航器中然后才能在这些页面之间进行切换跳转。</p><ol><li>创建几个页面</li></ol><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1gegg9l9hm9j308y03wdgn.jpg" alt="image-20200421144714011.png"></p><ol start="2"><li>将两个页面注册到导航器中</li></ol><p>我们在注册页面之前需要先创建导航器，在 React Navigation 中提供了多种导航器，我们首先使用最基本的 <code>Stack（栈）</code> 导航器。</p><p>需要先安装</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn add @react-navigation&#x2F;stack</span><br></pre></td></tr></table></figure><p>安装完之后注册：</p><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1gegg9zqp3mj31l210a4qp.jpg" alt="image-20200421145410757.png"></p><h2 id="页面的跳转"><a href="#页面的跳转" class="headerlink" title="页面的跳转"></a>页面的跳转</h2><p>要在页面之间跳转可以这样：</p><ol><li>在每个注册的页面中都会接收到一个属性：<code>navigation</code> 对象，在页面中可以直接使用</li><li>navigation 对象中提供了几个用来实现跳转的方法：</li></ol><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">navigation.navigate(&#39;页面的名称&#39;, 参数)</span><br><span class="line">	特点：1. 保留当前页面</span><br><span class="line">	     2. 跳转到目标页面，如果目标页面已经存在，就直接跳到这个页面不会新创建一个</span><br><span class="line">navigation.push(&#39;页面的名&#39;, 参数）</span><br><span class="line">	特点：1. 保留当前页面</span><br><span class="line">	     2. 创建一个新的页面，然后跳转过去</span><br><span class="line">navigation.goBack()</span><br><span class="line">	特点：1. 销毁当前页面</span><br><span class="line">		 2. 跳回上一个页面</span><br><span class="line">navigation.popToTop()</span><br><span class="line">	特点：1. 跳回到第一个页面</span><br><span class="line">		  2. 销毁所有其他的页面</span><br></pre></td></tr></table></figure><p>四个跳转的原理： Stack 导航器的四个跳转完全就是栈操作：</p><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggabijblj31040w21j1.jpg" alt="image-20200421151800181.png"></p><p>示例、使用 navigate 实现跳转</p><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggamy23hj31qc0m4aow.jpg" alt="image-20200421145944883.png"></p><p>效果：</p><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggavzajij30hk06ywf1.jpg" alt="image-20200421150027212.png"></p><p>跳转之后，下面个页面还自带一个后退按钮可以回来：</p><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggb3s0ssj30hs04sgls.jpg" alt="image-20200421150044477.png"></p><h2 id="传参数"><a href="#传参数" class="headerlink" title="传参数"></a>传参数</h2><p>我们经常在跳转页面时需要传参数，比如，点击一件商品时跳转到商品详情页时要传商品的ID等。</p><p>传参数：</p><ol><li>在调用 navigate 跳转时通过第二个参数传数据</li></ol><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggbdobymj313e0c8afl.jpg" alt="image-20200421153732352.png"></p><ol start="2"><li>在下一个页面中可以通过 <code>route</code> 属性中的 <code>params</code> 属性来进行接收</li></ol><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggbl9lr4j30ws0c8jw4.jpg" alt="image-20200421153904020.png"></p><p>传参数案例、在详情页面中放一个输入框，当点击发送按钮时，把输入框中的内容传回到首页。</p><ol start="3"><li>在详情页中制作按钮和输入框</li></ol><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggbrso43j31sy0uo4lf.jpg" alt="image-20200421155557803.png"></p><ol start="4"><li>在首页中接收数据并显示</li></ol><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggbxwwsuj31rk0p4aoz.jpg" alt="image-20200421155848030.png"></p><h2 id="配置标题栏"><a href="#配置标题栏" class="headerlink" title="配置标题栏"></a>配置标题栏</h2><p>在 Stack 导航器中的每个页面默认都带一个标题栏：</p><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggc5k306j30ji0f440q.jpg" alt="image-20200421160151687.png"></p><p>我们可以对这个这个标题栏进行配置，配置的代码可以写在两个地方：</p><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggchswmrj31780hg180.jpg" alt="image-20200421160343242.png"></p><p>我们只需要添加一个 <code>options</code> 的属性即可配置，在options中可以配置的项：</p><p>title：页面标题</p><p>headerStyle：标题栏的样式，比如：高度、背景色等</p><p>headerTintColor：标题文字的颜色</p><p>headerTitleStyle：标题文字的样式</p><h3 id="配置静态样式"><a href="#配置静态样式" class="headerlink" title="配置静态样式"></a>配置静态样式</h3><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggcpnmxdj31c60zckaq.jpg" alt="image-20200421160916349.png"></p><h3 id="动态的配置样式"><a href="#动态的配置样式" class="headerlink" title="动态的配置样式"></a>动态的配置样式</h3><p>我们在配置时也可以写一个函数，通过函数实现动态的配置。</p><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggcy3heoj31f00vwnhr.jpg" alt="image-20200421161423373.png"></p><h3 id="自定义组件替换标题"><a href="#自定义组件替换标题" class="headerlink" title="自定义组件替换标题"></a>自定义组件替换标题</h3><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggd7wr5lj31f212kx23.jpg" alt="image-20200421161836842.png"></p><h1 id="底部导航器"><a href="#底部导航器" class="headerlink" title="底部导航器"></a>底部导航器</h1><p>在 React Navigation 中提供了多种不同的导航器</p><p>制作手机 APP 一般都是有底部按钮：</p><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggdbnjetj30jo05i0u0.jpg" alt="image-20200421164509609.png"></p><h2 id="基本用法"><a href="#基本用法" class="headerlink" title="基本用法"></a>基本用法</h2><ol><li>安装</li></ol><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn add @react-navigation&#x2F;bottom-tabs</span><br></pre></td></tr></table></figure><ol start="2"><li>创建页面并注册</li></ol><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggdkr5rpj31mo0zekik.jpg" alt="image-20200421165207454.png"></p><h2 id="设置图标样式"><a href="#设置图标样式" class="headerlink" title="设置图标样式"></a>设置图标样式</h2><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggdxrl71j31j01bwe81.jpg" alt="image-20200421170830836.png"></p><h2 id="Tab-页面之间的跳转"><a href="#Tab-页面之间的跳转" class="headerlink" title="Tab 页面之间的跳转"></a>Tab 页面之间的跳转</h2><p>根据文档，直接使用 <code>navigation.navigate</code> 方法即可跳转。</p><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1gegge2c211j30wo0fowkj.jpg" alt="image-20200421171205967.png"></p><p>疑问：Tab 导航在切换时，是否会销毁当前页面，然后再跳转？</p><p>思路：可以使用生命周期函数测试一下，我们在组件创建时生成一个随机数，然后跳转之后再跳转回来看看随机数有没有改变，如果改变了说明每次跳转都要重新创建组件（每次跳转时被销毁了）。</p><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggei953gj31ns11c1kx.jpg" alt="image-20200421171714816.png"></p><p>经过测试，发现跳转之后再回来，随机数没变，也就是说明，在跳转 tab 跳转时并没有销毁上一个页面，只是隐藏起来了。</p><h1 id="导航器的嵌套使用"><a href="#导航器的嵌套使用" class="headerlink" title="导航器的嵌套使用"></a>导航器的嵌套使用</h1><p>实际开发中，我们经常需要多种导航器混合使用。</p><p>比如：最外层是一个抽屉导航器，然后其中某一个页面中又有一个底部导航器。</p><h2 id="示例一"><a href="#示例一" class="headerlink" title="示例一"></a>示例一</h2><p>比如：我们制作这样一个功能：</p><p>首页<br>​ 商品列表<br>​ 商品详情页 （返回）</p><p>通讯录<br>​ 用户列表<br>​ 用户聊天界面（返回）</p><p>我的<br>​ 订单列表<br>​ 订单详情（返回）</p><p>外层是底部导航，里面每个又是 Stack 导航器。</p><ol><li>先创建相应的 6 个页面</li></ol><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggetpz8ij307u0cgtax.jpg" alt="image-20200421175040924.png"></p><ol start="2"><li>使用 6 个页面创建三组 Stack 导航器，并把每一组页面封装成一个组件：</li></ol><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggez4ywhj30rs1527qq.jpg" alt="image-20200421180107957.png"></p><ol start="3"><li>再通过这三个组件构造出底部导航器</li></ol><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggf5tfv0j314m1347qg.jpg" alt="image-20200421180228972.png"></p><p>效果：每个里面还可以切换页面：</p><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggfd5bpdj30i20zg0wl.jpg" alt="image-20200421180249305.png"></p><p>也可切换：</p><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggfhugzjj30ho0voq4d.jpg" alt="image-20200421180321626.png"></p><h2 id="示例二"><a href="#示例二" class="headerlink" title="示例二"></a>示例二</h2><p>在我的页面中添加底部导航器再显示两个页面。</p><ol><li>为我的页面再创建两个页</li></ol><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggfrc2m9j30di08mjt9.jpg" alt="image-20200422111637641.png"></p><ol start="2"><li>创建一个底部导航器把这两个页面管理起来，封装成一个组件</li></ol><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggfzm1w3j312k0g4tju.jpg" alt="image-20200422111941616.png"></p><ol start="3"><li>把这个组件放到抽屉导航器中</li></ol><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggg6jrx4j316i0piapv.jpg" alt="image-20200422112407042.png"></p><p>效果：外层是抽屉导航器，然后我的页面中包含一个底部导航器：</p><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggger2nsj30ii0t4405.jpg" alt="image-20200422112440900.png"></p><h2 id="示例三"><a href="#示例三" class="headerlink" title="示例三"></a>示例三</h2><p>在详情页中嵌套一个顶部导航器，分别显示基本信息，评论，售后三个页面。</p><ol><li>先创建三个页面</li></ol><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggijx9fgj30ae07agn2.jpg" alt="image-20200422112841410.png"></p><ol start="2"><li>使用顶部导航器把这三个页面管理起来并封装成一个组件</li></ol><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggipjrxkj30ts0uo1ap.jpg" alt="image-20200422113130197.png"></p><ol start="3"><li>把这个组件嵌套到 Drawer 导航器中</li></ol><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggitozccj314u1601ij.jpg" alt="image-20200422113225014.png"></p><p>效果：</p><p><img src="http://ww1.sinaimg.cn/large/007WurYGgy1geggiygbnhj30i20rymyl.jpg" alt="image-20200422113324038.png"></p><p>最终代码</p><p>app.js</p><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">'react'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; StyleSheet, View, Text &#125; <span class="keyword">from</span> <span class="string">'react-native'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 引入导航容器</span></span><br><span class="line"><span class="keyword">import</span> &#123; NavigationContainer &#125; <span class="keyword">from</span> <span class="string">'@react-navigation/native'</span></span><br><span class="line"><span class="comment">// 引入五个导航器</span></span><br><span class="line"><span class="keyword">import</span> &#123; createStackNavigator &#125; <span class="keyword">from</span> <span class="string">'@react-navigation/stack'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; createDrawerNavigator &#125; <span class="keyword">from</span> <span class="string">'@react-navigation/drawer'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; createBottomTabNavigator &#125; <span class="keyword">from</span> <span class="string">'@react-navigation/bottom-tabs'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; createMaterialBottomTabNavigator &#125; <span class="keyword">from</span> <span class="string">'@react-navigation/material-bottom-tabs'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; createMaterialTopTabNavigator &#125; <span class="keyword">from</span> <span class="string">'@react-navigation/material-top-tabs'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 创建导航器组件</span></span><br><span class="line"><span class="keyword">const</span> DrawerNavigator = createDrawerNavigator()</span><br><span class="line"><span class="keyword">const</span> BottomNavigator = createBottomTabNavigator()</span><br><span class="line"><span class="keyword">const</span> TopTabNavigator = createMaterialTopTabNavigator()</span><br><span class="line"></span><br><span class="line"><span class="comment">// 引入页面</span></span><br><span class="line"><span class="keyword">import</span> Index <span class="keyword">from</span> <span class="string">'./pages/Index'</span></span><br><span class="line"><span class="keyword">import</span> Me <span class="keyword">from</span> <span class="string">'./pages/me/Me'</span></span><br><span class="line"><span class="keyword">import</span> Order <span class="keyword">from</span> <span class="string">'./pages/me/Order'</span></span><br><span class="line"><span class="keyword">import</span> Detail <span class="keyword">from</span> <span class="string">'./pages/detail/Detail'</span></span><br><span class="line"><span class="keyword">import</span> AfterSale <span class="keyword">from</span> <span class="string">'./pages/detail/AfterSale'</span></span><br><span class="line"><span class="keyword">import</span> Comment <span class="keyword">from</span> <span class="string">'./pages/detail/Comment'</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 详情路由组件</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">DetailScreen</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;TopTabNavigator.Navigator&gt;</span><br><span class="line">      &lt;TopTabNavigator.Screen</span><br><span class="line">        name=<span class="string">'BaseInfo'</span></span><br><span class="line">        component=&#123;Detail&#125;</span><br><span class="line">        options=&#123;&#123; <span class="attr">title</span>: <span class="string">'基本信息'</span> &#125;&#125;</span><br><span class="line">      /&gt;</span><br><span class="line">      &lt;TopTabNavigator.Screen</span><br><span class="line">        name=<span class="string">'AfterSale'</span></span><br><span class="line">        component=&#123;AfterSale&#125;</span><br><span class="line">        options=&#123;&#123; <span class="attr">title</span>: <span class="string">'售后'</span> &#125;&#125;</span><br><span class="line">      /&gt;</span><br><span class="line">      &lt;TopTabNavigator.Screen</span><br><span class="line">        name=<span class="string">'Comment'</span></span><br><span class="line">        component=&#123;Comment&#125;</span><br><span class="line">        options=&#123;&#123; <span class="attr">title</span>: <span class="string">'评论'</span> &#125;&#125;</span><br><span class="line">      /&gt;</span><br><span class="line">    &lt;<span class="regexp">/TopTabNavigator.Navigator&gt;</span></span><br><span class="line"><span class="regexp">  )</span></span><br><span class="line"><span class="regexp">&#125;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">/</span><span class="regexp">/ 个人中心路由组件</span></span><br><span class="line"><span class="regexp">function MyScreen() &#123;</span></span><br><span class="line"><span class="regexp">  return (</span></span><br><span class="line"><span class="regexp">    &lt;BottomNavigator.Navigator&gt;</span></span><br><span class="line"><span class="regexp">      &lt;BottomNavigator.Screen</span></span><br><span class="line"><span class="regexp">        name='Me'</span></span><br><span class="line"><span class="regexp">        component=&#123;Me&#125;</span></span><br><span class="line"><span class="regexp">        options=&#123;&#123; title: '我的' &#125;&#125;</span></span><br><span class="line"><span class="regexp">      /</span>&gt;</span><br><span class="line">      &lt;BottomNavigator.Screen</span><br><span class="line">        name=<span class="string">'Order'</span></span><br><span class="line">        component=&#123;Order&#125;</span><br><span class="line">        options=&#123;&#123; <span class="attr">title</span>: <span class="string">'订单'</span> &#125;&#125;</span><br><span class="line">      /&gt;</span><br><span class="line">    &lt;<span class="regexp">/BottomNavigator.Navigator&gt;</span></span><br><span class="line"><span class="regexp">  )</span></span><br><span class="line"><span class="regexp">&#125;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">const App = () =&gt; &#123;</span></span><br><span class="line"><span class="regexp">  return (</span></span><br><span class="line"><span class="regexp">    &lt;NavigationContainer&gt;</span></span><br><span class="line"><span class="regexp">      &lt;DrawerNavigator.Navigator&gt;</span></span><br><span class="line"><span class="regexp">        &lt;DrawerNavigator.Screen</span></span><br><span class="line"><span class="regexp">          name="Index"</span></span><br><span class="line"><span class="regexp">          component=&#123;Index&#125;</span></span><br><span class="line"><span class="regexp">          options=&#123;&#123; title: '首页' &#125;&#125;</span></span><br><span class="line"><span class="regexp">        /</span>&gt;</span><br><span class="line">        &lt;DrawerNavigator.Screen</span><br><span class="line">          name=<span class="string">"Detail"</span></span><br><span class="line">          component=&#123;DetailScreen&#125;</span><br><span class="line">          options=&#123;&#123; <span class="attr">title</span>: <span class="string">'详情'</span> &#125;&#125;</span><br><span class="line">        /&gt;</span><br><span class="line">        &lt;DrawerNavigator.Screen</span><br><span class="line">          name=<span class="string">"Me"</span></span><br><span class="line">          component=&#123;MyScreen&#125;</span><br><span class="line">          options=&#123;&#123; <span class="attr">title</span>: <span class="string">'我的'</span> &#125;&#125;</span><br><span class="line">        /&gt;</span><br><span class="line">      &lt;<span class="regexp">/DrawerNavigator.Navigator&gt;</span></span><br><span class="line"><span class="regexp">    &lt;/</span>NavigationContainer&gt;</span><br><span class="line">  )</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> styles = StyleSheet.create(&#123;&#125;);</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> App;</span><br></pre></td></tr></table></figure></div><footer class="post-footer"><div class="post-tags"><a href="/tags/ReactNative/" rel="tag"><i class="fa fa-tag"></i> ReactNative</a></div><div class="post-nav"><div class="post-nav-next post-nav-item"><a href="/article/4995992c.html" rel="next" title="ReactNative -- 入门"><i class="fa fa-chevron-left"></i> ReactNative -- 入门</a></div><span class="post-nav-divider"></span><div class="post-nav-prev post-nav-item"> <a href="/article/ca16d5cb.html" rel="prev" title="ReactNative -- 数据共享">ReactNative -- 数据共享<i class="fa fa-chevron-right"></i></a></div></div></footer></div></article><div class="post-spread"></div></div></div><div class="comments" id="comments"></div></div><div class="sidebar-toggle"><div class="sidebar-toggle-line-wrap"><span class="sidebar-toggle-line sidebar-toggle-line-first"></span><span class="sidebar-toggle-line sidebar-toggle-line-middle"></span><span class="sidebar-toggle-line sidebar-toggle-line-last"></span></div></div><aside id="sidebar" class="sidebar"><div class="sidebar-inner"><ul class="sidebar-nav motion-element"><li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap"> 文章目录</li><li class="sidebar-nav-overview" data-target="site-overview-wrap"> 站点概览</li></ul><section class="site-overview-wrap sidebar-panel"><div class="site-overview"><div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person"> <img class="site-author-image" itemprop="image" src="http://ww1.sinaimg.cn/large/007WurYGgy1ge8nxttd9nj302s02s0si.jpg" alt="King-XiYu"><p class="site-author-name" itemprop="name">King-XiYu</p><p class="site-description motion-element" itemprop="description">有志者事竟成</p></div><nav class="site-state motion-element"><div class="site-state-item site-state-posts"> <a href="/archives/"><span class="site-state-item-count">37</span> <span class="site-state-item-name">日志</span></a></div><div class="site-state-item site-state-categories"> <a href="/categories/index.html"><span class="site-state-item-count">11</span> <span class="site-state-item-name">分类</span></a></div><div class="site-state-item site-state-tags"> <a href="/tags/index.html"><span class="site-state-item-count">10</span> <span class="site-state-item-name">标签</span></a></div></nav><div class="feed-link motion-element"><a href="/atom.xml" rel="alternate"><i class="fa fa-rss"></i> RSS</a></div><div class="links-of-author motion-element"><span class="links-of-author-item"><a href="https://gitee.com/King-XiYu" target="_blank" title="Gitee"><i class="fa fa-fw fa-hand-o-right"></i> Gitee</a></span><span class="links-of-author-item"><a href="https://github.com/King-XiYu" target="_blank" title="GitHub"><i class="fa fa-fw fa-github"></i> GitHub</a></span></div><div class="links-of-blogroll motion-element links-of-blogroll-inline"><div class="links-of-blogroll-title"><i class="fa fa-fw fa-link"></i> Links</div><ul class="links-of-blogroll-list"><li class="links-of-blogroll-item"> <a href="https://www.baidu.com/" title="度娘" target="_blank">度娘</a></li><li class="links-of-blogroll-item"> <a href="https://www.google.com/" title="Google" target="_blank">Google</a></li></ul></div></div></section><section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active"><div class="post-toc"><div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#介绍"><span class="nav-number">1.</span> <span class="nav-text">介绍</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#安装"><span class="nav-number">2.</span> <span class="nav-text">安装</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#五种导航器"><span class="nav-number">3.</span> <span class="nav-text">五种导航器</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#注意"><span class="nav-number">3.1.</span> <span class="nav-text">注意</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#示例"><span class="nav-number">3.2.</span> <span class="nav-text">示例</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-创建三个页面"><span class="nav-number">3.2.1.</span> <span class="nav-text">1. 创建三个页面</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-安装五种导航器"><span class="nav-number">3.2.2.</span> <span class="nav-text">2. 安装五种导航器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-引入导航器容器，套在最外层"><span class="nav-number">3.2.3.</span> <span class="nav-text">3. 引入导航器容器，套在最外层</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-引入五个导航器（用哪个引哪个）"><span class="nav-number">3.2.4.</span> <span class="nav-text">4. 引入五个导航器（用哪个引哪个）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-创建导航器组件（用哪个创建哪个，这里我们先试一下-Drawer）"><span class="nav-number">3.2.5.</span> <span class="nav-text">5. 创建导航器组件（用哪个创建哪个，这里我们先试一下 Drawer）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-引入所有的页面"><span class="nav-number">3.2.6.</span> <span class="nav-text">6. 引入所有的页面</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#7-使用这个组件来管理页面"><span class="nav-number">3.2.7.</span> <span class="nav-text">7. 使用这个组件来管理页面</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#效果："><span class="nav-number">3.2.8.</span> <span class="nav-text">效果：</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#栈导航器"><span class="nav-number">4.</span> <span class="nav-text">栈导航器</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#注册页面"><span class="nav-number">4.1.</span> <span class="nav-text">注册页面</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#页面的跳转"><span class="nav-number">4.2.</span> <span class="nav-text">页面的跳转</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#传参数"><span class="nav-number">4.3.</span> <span class="nav-text">传参数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#配置标题栏"><span class="nav-number">4.4.</span> <span class="nav-text">配置标题栏</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#配置静态样式"><span class="nav-number">4.4.1.</span> <span class="nav-text">配置静态样式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#动态的配置样式"><span class="nav-number">4.4.2.</span> <span class="nav-text">动态的配置样式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#自定义组件替换标题"><span class="nav-number">4.4.3.</span> <span class="nav-text">自定义组件替换标题</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#底部导航器"><span class="nav-number">5.</span> <span class="nav-text">底部导航器</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#基本用法"><span class="nav-number">5.1.</span> <span class="nav-text">基本用法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#设置图标样式"><span class="nav-number">5.2.</span> <span class="nav-text">设置图标样式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Tab-页面之间的跳转"><span class="nav-number">5.3.</span> <span class="nav-text">Tab 页面之间的跳转</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#导航器的嵌套使用"><span class="nav-number">6.</span> <span class="nav-text">导航器的嵌套使用</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#示例一"><span class="nav-number">6.1.</span> <span class="nav-text">示例一</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#示例二"><span class="nav-number">6.2.</span> <span class="nav-text">示例二</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#示例三"><span class="nav-number">6.3.</span> <span class="nav-text">示例三</span></a></li></ol></li></ol></div></div></section><div class="back-to-top"><i class="fa fa-arrow-up"></i> <span id="scrollpercent"><span>0</span>%</span></div></div></aside></div></main><footer id="footer" class="footer"><div class="footer-inner"><div class="copyright">&copy; <span itemprop="copyrightYear">2020</span><span class="with-love"><i class="fa fa-user"></i></span> <span class="author" itemprop="copyrightHolder">宇宙特别帅</span></div></div></footer></div><script type="text/javascript">"[object Function]"!==Object.prototype.toString.call(window.Promise)&&(window.Promise=null)</script><script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script><script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script><script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script><script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script><script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script><script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script><script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script><script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script><script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script><script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script><script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script><script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script><script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script><script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script><script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script><script src="//cdn.jsdelivr.net/npm/valine/dist/Valine.min.js"></script><script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: true,
        appId: '9DODHXKj6rr1cK4oO07iVBX3-gzGzoHsz',
        appKey: 'aJaxxns4VN5WV9nMimT9aSb1',
        placeholder: 'ヾﾉ≧∀≦)o 来呀！吐槽一番吧！',
        avatar:'mp',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script><script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "./public/search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script><script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script><script>AV.initialize("9DODHXKj6rr1cK4oO07iVBX3-gzGzoHsz","aJaxxns4VN5WV9nMimT9aSb1")</script><script>function showTime(e){var t=new AV.Query(e),c=[],u=$(".leancloud_visitors");u.each(function(){c.push($(this).attr("id").trim())}),t.containedIn("url",c),t.find().done(function(e){var t=".leancloud-visitors-count";if(0!==e.length){for(var n=0;n<e.length;n++){var o=e[n],i=o.get("url"),s=o.get("time"),r=document.getElementById(i);$(r).find(t).text(s)}for(n=0;n<c.length;n++){i=c[n],r=document.getElementById(i);var l=$(r).find(t);""==l.text()&&l.text(0)}}else u.find(t).text(0)}).fail(function(e,t){console.log("Error: "+t.code+" "+t.message)})}function addCount(i){var e=$(".leancloud_visitors"),s=e.attr("id").trim(),r=e.attr("data-flag-title").trim(),t=new AV.Query(i);t.equalTo("url",s),t.find({success:function(e){if(0<e.length){var t=e[0];t.fetchWhenSave(!0),t.increment("time"),t.save(null,{success:function(e){$(document.getElementById(s)).find(".leancloud-visitors-count").text(e.get("time"))},error:function(e,t){console.log("Failed to save Visitor num, with error message: "+t.message)}})}else{var n=new i,o=new AV.ACL;o.setPublicReadAccess(!0),o.setPublicWriteAccess(!0),n.setACL(o),n.set("title",r),n.set("url",s),n.set("time",1),n.save(null,{success:function(e){$(document.getElementById(s)).find(".leancloud-visitors-count").text(e.get("time"))},error:function(e,t){console.log("Failed to create")}})}},error:function(e){console.log("Error:"+e.code+" "+e.message)}})}$(function(){var e=AV.Object.extend("Counter");1==$(".leancloud_visitors").length?addCount(e):1<$(".post-title-link").length&&showTime(e)})</script><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.4/clipboard.min.js"></script><script type="text/javascript" src="/js/src/clipboard-use.js"></script></body></html>